#include <iostream>
#include "globals.h"
#include "Map.h"

void Map::_map() {
	
}

Map::Map(const char* cfile):Titleset(cfile) {
	_map();
}

Map::Map(const string& sfile):Titleset(sfile) {
	_map();
}

Map::~Map() {
	//Titleset::~Titleset();
}

bool Map::init() {
	bool res=Titleset::init();
	if(!res) return res;
	return read_map();	
}

bool Map::read_map() {
	string file2=file+".map";
	inputStream.open(file2.c_str(),ios::in);
	if( !inputStream ) {
		cerr << "Error opening " << file2 << " ." << endl;
		if(!file_exist(file2.c_str())) {
			cerr << "file doesn't exist !" << endl;
		}
		inputStream.close();
		return false;
	}
	string line="";
	inputStream >> line;
	if( line.compare("MAPv2")!=0 ) {
		cerr << "Wrong map version" << endl;
		inputStream.close();
		return false;
	}
	inputStream >> mx >> my >> sx >> sy >> px >> py >> p;
	if(mx>0&&my>0) {
		map.clear();
		//map.swap(spmap());
	}
	string temp;//possible move on the cells
	for(int i=0;i<mx;i++) {
		map.push_back(stv());
		for(int j=0;j<my;j++) {
			inputStream>>temp;
			map[i].push_back(temp);
		}
	}
	if(p>0) {
		perso_pos.clear();
		//perso_pos.swap( pos_t() );
	}
	pos_t rec;//position of the player on the map
	for(unsigned int i=0;i<p;i++) {
		inputStream >> rec.x >> rec.y;
	}
	inputStream >> line;
	if( line.compare("end")!=0 ) {
		cerr << "Wrong map end" << endl;
		inputStream.close();
		return false;
	}
	fflush(stdout);
	inputStream.close();
	return true;
}

int Map::GetMapSizeX() const {
	return mx;
}

int Map::GetMapSizeY() const {
	return my;
}

int Map::GetScreenSizeX() const {
	return sx;
}

int Map::GetScreenSizeY() const {
	return sy;
}

bool Map::add_player(Perso &pe) {
	if(persos.size()<p) {
		persos.push_back(&pe);
		pe.set_map(*this);
		return true;
	}
	return false;
}

void Map::draw_overlay_layer(unsigned int l) const {
	for(unsigned int i=0;i<layers_over.at(l).size();i++) {
		for(unsigned int j=0;j<persos.size();j++) {
			if(
			persos.at(j)->GetPosX() <= layers_over.at(l).at(i).GetSubRect().Right/move_choice // position gauche perso <= position droite layer
			&&
			persos.at(j)->GetPosX()+(persos.at(j)->GetSizeX()-1) >= layers_over.at(l).at(i).GetSubRect().Left/move_choice // position droite perso >= position gauche layer
			&&
			persos.at(j)->GetPosY() <= layers_over.at(l).at(i).GetSubRect().Bottom/move_choice // position haute perso < position base layer
			&&
			//persos.at(j)->GetPosY()+(persos.at(j)->GetSizeY()-1) >= layers_over.at(l).at(i).GetSubRect().Top/move_choice // position base perso >= position haute layer
			persos.at(j)->GetPosY()+(persos.at(j)->GetSizeY()-1) < layers_over.at(l).at(i).GetSubRect().Bottom/move_choice // position base perso < position base layer
			)
				App.Draw( layers_over.at(l).at(i) );
		}
	}
}

void Map::draw_overlay() const {
	unsigned int _m=1;
	for(unsigned int i=0;i<layers.size();i++) {
		if( (mask&_m) == _m ) draw_overlay_layer(i);
		_m<<=1;
	}
}
